package 题目集.dfs.洪水填充;

import org.junit.Test;

/**
 * https://leetcode.cn/problems/bricks-falling-when-hit/description/
 * todo:未完成
 */
public class 打砖块_时光倒流技巧 {
    int[][] grid;
    static int[][] dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

    public int[] hitBricks(int[][] grid, int[][] hits) {
        this.grid = grid;
        int[] res = new int[hits.length];
        for (int i = 0; i < res.length; i++) {
            res[i] = dfs(hits[i][0], hits[i][1]);
            res[i] = res[i] == 0 ? 0 : res[i] - 1;
        }
        return res;
    }

    public int dfs(int x, int y) {
        if (grid[x][y] == 0) return 0;
        grid[x][y] = 0;
        int res = 1;
        for (int[] dir : dirs) {
            int nx = x + dir[0], ny = y + dir[1];
            // nx >= 1 保证不会把第一行的砖块打掉
            if (nx >= 1 && nx < grid.length && ny >= 0 && ny < grid[0].length) {
                res += dfs(nx, ny);
            }
        }
        return res;
    }
    @Test
    public void test(){
        int[][] grid = {{1,0,0,0},{1,1,0,0}};
        int[][] hits = {{1,1},{1,0}};
        int[] res = hitBricks(grid, hits);
        for (int re : res) {
            System.out.println(re);
        }
    }
}
